package com.example.ybg.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;


public class MainActivity extends Activity {
    private  static final int GET_ALL_APP_FINISH = 1;

    private ListView lv_app_manager;

    private AppInfoService as;

    private List<Appinfo> list;

    private AppManagerAdapter adapter;

    private LinearLayout progressbar;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg){
            switch (msg.what){
                case GET_ALL_APP_FINISH:
                    progressbar.setVisibility(View.GONE);
                    adapter = new AppManagerAdapter();
                    lv_app_manager.setAdapter(adapter);
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);
        progressbar =  (LinearLayout) findViewById(R.id.ll_app_manager_progress);
        new Thread(){
            public void run(){
                as = new AppInfoService(MainActivity.this);
                list = as.getAppinfos();
                Message msg = new Message();
                msg.what = GET_ALL_APP_FINISH;
                handler.sendMessage(msg);
            }
        }.start();

    }
    private class AppManagerAdapter extends BaseAdapter{
        public int getCount(){
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           Appinfo info = list.get(position);
            if(convertView == null){
                View view = View.inflate(MainActivity.this,R.layout.applationitem,null);
                AppManagerViews views = new AppManagerViews();
                views.iv = (ImageView) view.findViewById(R.id.ImageView);
                views.tv = (TextView) view.findViewById(R.id.textView);
                view.setTag(views);
                return view;

            }else{
                AppManagerViews views = (AppManagerViews) convertView.getTag();
                views.iv.setImageDrawable(info.getApp_icon());
                views.tv.setText(info.getApp_name());
                return convertView;
            }
        }
    }
    private class AppManagerViews{
        ImageView iv;
        TextView tv;
    }








}
